/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmFacturas.java
 *
 * Created on 10-oct-2011, 9:25:08
 */
package apiformularios.form;

import api.dom.Camion;
import api.dom.Cliente;
import api.dom.Empleado;
import api.dom.Factura;
import api.dom.Utilitaria;
import api.gest.GestCambio;
import api.gest.GestFactura;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import net.sf.jasperreports.engine.design.*;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.*;
import javax.swing.*;
import net.sf.jasperreports.view.JasperViewer;
import server.dom.BaseDeDatos;

/**
 *
 * @author Luis
 */
public class FrmFacturas extends javax.swing.JInternalFrame {

    Connection con = null;
    Boolean cli = false;

    /** Creates new form FrmFacturas */
    public FrmFacturas() {
        initComponents();
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        //this.txtNumero.setText(String.valueOf(GestFactura.getInstance().devolverUltimoNum()));
        labelnumero.setText(String.valueOf(GestFactura.getInstance().devolverUltimoNum()));
        jComboBox2.setModel(Utilitaria.listarCamionesCombo((DefaultComboBoxModel) jComboBox2.getModel()));
        jComboBox3.setModel(Utilitaria.listarEmpleadosCombo((DefaultComboBoxModel) jComboBox3.getModel()));
        jComboBox4.setModel(Utilitaria.listarClientesCombo((DefaultComboBoxModel) jComboBox4.getModel()));
        jComboBox2.setSelectedItem(null);
        jComboBox3.setSelectedItem(null);
        jComboBox4.setSelectedItem(null);
        //   this.jTable1.setModel(Utilitaria.listarFacturas((DefaultTableModel) jTable1.getModel()));
        JFormattedTextField txtNombre1 = new JFormattedTextField();
        this.txttipocambio.setText("0");
        txtNombre1.setValue(new Integer(3));
        Date hoy = new Date();
        jDateChooser1.setDate(hoy);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtLugardecarga = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        txtKm = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        txtLugardedescarga = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtKmretorno = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtGrupometros = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtCantidadbultos = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtCantanimales = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtGuia = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtImporte = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        txtobservaciones = new javax.swing.JTextField();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jPanel1 = new javax.swing.JPanel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        labelnumero = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        btnEliminar = new javax.swing.JButton();
        btnatras = new javax.swing.JButton();
        btnAgregar1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        txtNombre1 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jComboBox3 = new javax.swing.JComboBox();
        jComboBox4 = new javax.swing.JComboBox();
        jLabel18 = new javax.swing.JLabel();
        btnBuscar = new javax.swing.JButton();
        btnBuscar1 = new javax.swing.JButton();
        txttipocambio = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        jLabel29 = new javax.swing.JLabel();
        jLabel30 = new javax.swing.JLabel();
        jLabel31 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setNextFocusableComponent(jDateChooser1);
        setNormalBounds(new java.awt.Rectangle(0, 0, 650, 0));
        setPreferredSize(new java.awt.Dimension(650, 588));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Fecha :");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Moneda: ");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 80, -1, 20));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("L. Carga :");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 200, -1, -1));

        txtLugardecarga.setNextFocusableComponent(txtLugardedescarga);
        getContentPane().add(txtLugardecarga, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 200, 180, -1));

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Numero", "Fecha", "Matricula Camion", "Cliente", "Chofer"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setEnabled(false);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jTable1MouseExited(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 350, 630, 160));

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setText("Km :");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 260, -1, -1));

        txtKm.setNextFocusableComponent(txtKmretorno);
        txtKm.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtKmKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtKmKeyTyped(evt);
            }
        });
        getContentPane().add(txtKm, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 260, 180, -1));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pesos", "Dolares" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 80, 200, 20));

        txtLugardedescarga.setNextFocusableComponent(txtKm);
        getContentPane().add(txtLugardedescarga, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 230, 180, -1));

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel8.setText("Km retorno :");
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 290, -1, -1));

        txtKmretorno.setNextFocusableComponent(txtImporte);
        txtKmretorno.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtKmretornoKeyTyped(evt);
            }
        });
        getContentPane().add(txtKmretorno, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 290, 180, -1));

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setText("Metros :");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 260, -1, -1));

        txtGrupometros.setNextFocusableComponent(jComboBox2);
        txtGrupometros.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtGrupometrosKeyTyped(evt);
            }
        });
        getContentPane().add(txtGrupometros, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 260, 200, -1));

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel10.setText("Cant Bultos :");
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 170, -1, -1));

        txtCantidadbultos.setNextFocusableComponent(txtCantanimales);
        txtCantidadbultos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCantidadbultosKeyTyped(evt);
            }
        });
        getContentPane().add(txtCantidadbultos, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 170, 200, -1));

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel11.setText("Cant Animales :");
        getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 200, -1, -1));

        txtCantanimales.setNextFocusableComponent(txtobservaciones);
        txtCantanimales.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCantanimalesKeyTyped(evt);
            }
        });
        getContentPane().add(txtCantanimales, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 200, 200, -1));

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel12.setText("Tipo de Cambio:");
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 110, -1, -1));

        txtGuia.setNextFocusableComponent(txtCantidadbultos);
        getContentPane().add(txtGuia, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 140, 200, -1));

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel13.setText("Importe :");
        getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 320, -1, -1));

        txtImporte.setNextFocusableComponent(txtGuia);
        txtImporte.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtImporteKeyTyped(evt);
            }
        });
        getContentPane().add(txtImporte, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 320, 180, -1));

        jLabel14.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel14.setText("Observaciones: ");
        getContentPane().add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 230, -1, -1));

        txtobservaciones.setNextFocusableComponent(txtGrupometros);
        getContentPane().add(txtobservaciones, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 230, 200, -1));

        jDateChooser1.setNextFocusableComponent(txtLugardecarga);
        getContentPane().add(jDateChooser1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 80, 180, 20));

        jPanel1.setBackground(new java.awt.Color(153, 180, 209));
        jPanel1.setPreferredSize(new java.awt.Dimension(580, 71));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel15.setBackground(new java.awt.Color(51, 255, 51));
        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabel15.setText("Facturas");
        jLabel15.setToolTipText("");
        jPanel1.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, 49));

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel16.setText("Lista Detallada :");
        jPanel1.add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 30, -1, -1));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/window-new.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 20, 30, 30));

        labelnumero.setFont(new java.awt.Font("Tahoma", 0, 36));
        labelnumero.setText("0");
        jPanel1.add(labelnumero, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 20, 220, 40));

        jLabel27.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabel27.setText("#");
        jPanel1.add(jLabel27, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 20, 60, 40));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 650, 70));

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        getContentPane().add(btnEliminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 520, 80, 40));

        btnatras.setText("Cerrar");
        btnatras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnatrasActionPerformed(evt);
            }
        });
        getContentPane().add(btnatras, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 520, 80, 40));

        btnAgregar1.setText("Agregar");
        btnAgregar1.setNextFocusableComponent(btnEliminar);
        btnAgregar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregar1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnAgregar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 520, 80, 40));

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        jPanel2.add(txtNombre1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 150, -1));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 520, -1, 40));

        jComboBox2.setNextFocusableComponent(jComboBox3);
        getContentPane().add(jComboBox2, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 110, 180, 20));

        jLabel6.setText("Camion :");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, -1, 20));

        jLabel17.setText("Chofer :");
        getContentPane().add(jLabel17, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, -1, 20));

        jComboBox3.setNextFocusableComponent(jComboBox4);
        getContentPane().add(jComboBox3, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 140, 180, -1));

        jComboBox4.setNextFocusableComponent(btnAgregar1);
        jComboBox4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox4ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox4, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 170, 180, -1));

        jLabel18.setText("Cliente : ");
        getContentPane().add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 170, -1, -1));

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
        getContentPane().add(btnBuscar, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 520, 90, 40));

        btnBuscar1.setText("...");
        btnBuscar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscar1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnBuscar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 520, 20, 40));

        txttipocambio.setEditable(false);
        getContentPane().add(txttipocambio, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 110, 200, -1));

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel19.setText("Guia :");
        getContentPane().add(jLabel19, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 140, -1, -1));

        jLabel20.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel20.setText("L. Descarga :");
        getContentPane().add(jLabel20, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 230, -1, -1));

        jLabel21.setFont(new java.awt.Font("Tahoma", 2, 12));
        jLabel21.setText("Campos Obligatorios (*)");
        getContentPane().add(jLabel21, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 330, 140, -1));

        jLabel22.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel22.setText("*");
        getContentPane().add(jLabel22, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 320, 20, -1));

        jLabel23.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel23.setText("*");
        getContentPane().add(jLabel23, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 80, 10, -1));

        jLabel24.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel24.setText("*");
        getContentPane().add(jLabel24, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 230, 20, -1));

        jLabel25.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel25.setText("*");
        getContentPane().add(jLabel25, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 260, 20, -1));

        jLabel26.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel26.setText("*");
        getContentPane().add(jLabel26, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 290, 20, -1));

        jLabel28.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel28.setText("*");
        getContentPane().add(jLabel28, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 200, 10, -1));

        jLabel29.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel29.setText("*");
        getContentPane().add(jLabel29, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 170, 10, -1));

        jLabel30.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel30.setText("*");
        getContentPane().add(jLabel30, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 140, 10, -1));

        jLabel31.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel31.setText("*");
        getContentPane().add(jLabel31, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 110, 10, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnatrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnatrasActionPerformed
    }//GEN-LAST:event_btnatrasActionPerformed

    private void btnAgregar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregar1ActionPerformed
        // TODO add your handling code here:
        try {
            if ("".equals(labelnumero.getText()) || "".equals(txtKm.getText()) || "".equals(txtImporte.getText()) || "".equals(txtLugardecarga.getText()) || "".equals(txtLugardedescarga.getText()) || "".equals(txtKmretorno.getText())) {
                JOptionPane.showMessageDialog(rootPane, "Falto ingresar algun dato obligatorio .\n Recuerde que los datos obligatorios son los datos de la columna de la izquierda");
            } else if (jComboBox2.getSelectedItem() == null) {
                JOptionPane.showMessageDialog(rootPane, "Seleccione un camion");
            } else if (jComboBox3.getSelectedItem() == null) {
                JOptionPane.showMessageDialog(rootPane, "Seleccione un chofer");
            } else if (jComboBox4.getSelectedItem() == null) {
                JOptionPane.showMessageDialog(rootPane, "Seleccione un cliente");
            } else {
                //convierte la fecha
                Date date = new Date();
                date = (jDateChooser1.getDate());
                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                String fecha = dateFormat.format(date);
                //convierte los objetos
                Camion camion = (Camion) jComboBox2.getSelectedItem();
                Empleado chofer = (Empleado) jComboBox3.getSelectedItem();
                Cliente cliente = (Cliente) jComboBox4.getSelectedItem();
                if ("".equals(this.txtCantanimales.getText())) {
                    this.txtCantanimales.setText("0");
                }
                if ("".equals(this.txtCantidadbultos.getText())) {
                    this.txtCantidadbultos.setText("0");
                }
                if ("".equals(this.txtGrupometros.getText())) {
                    this.txtGrupometros.setText("0");
                }
                if ("".equals(this.txtobservaciones.getText())) {
                    this.txtobservaciones.setText("Ninguna");
                }
                if ("".equals(this.txtGuia.getText())) {
                    this.txtGuia.setText("0");
                }
                //Convierto todos los datos antes de pasarselos a la factura !
                int numerofactura = Integer.parseInt(labelnumero.getText());
                int saldo = 0;
                int importe = Integer.parseInt(txtImporte.getText());
                double tipoc = Double.parseDouble(txttipocambio.getText());
                String moneda = jComboBox1.getSelectedItem().toString();
                String lugarcarga = txtLugardecarga.getText();
                String lugardescarga = txtLugardedescarga.getText();
                int km = Integer.parseInt(txtKm.getText());
                int metros = Integer.parseInt(txtGrupometros.getText());
                int cantB = Integer.parseInt(txtCantidadbultos.getText());
                int cantA = Integer.parseInt(txtCantanimales.getText());
                String guia = txtGuia.getText();
                int kmretorno = Integer.parseInt(txtKmretorno.getText());
                String observaciones = txtobservaciones.getText();
                //transformo el saldo si esta en dolares a pesos
                if (jComboBox1.getSelectedItem().toString().equals("Pesos")) {
                    saldo = (int) (importe);
                } else {
                    saldo = (int)(importe*tipoc);
                }
                //crea la factura
                Factura objFac = new Factura(numerofactura, fecha, moneda, tipoc, lugarcarga, lugardescarga, km, metros, cantB, cantA, guia, importe, cliente, chofer, camion, kmretorno, observaciones, saldo);
                GestFactura.getInstance().altaFactura(objFac);
                //muestra msj
                JOptionPane.showMessageDialog(null, "FACTURA AGREGADA EXITOSAMENTE.");
                //pregunta si desea imprimir
                if (JOptionPane.showConfirmDialog(rootPane, "Desea Imprimir factura?") == 0) {
                    try {
                        con = BaseDeDatos.getConexion();
                        JasperDesign jd = JRXmlLoader.load("iReport\\javo.jrxml");
                        String x = "Select * from facturas where fNumero=" + objFac.getfNumero() + "";
                        JRDesignQuery consulta = new JRDesignQuery();
                        consulta.setText(x);
                        jd.setQuery(consulta);
                        JasperReport jr = JasperCompileManager.compileReport(jd);
                        JasperPrint jp = JasperFillManager.fillReport(jr, null, con);
                        JasperViewer.viewReport(jp, false);
                    } catch (Exception e) {
                        System.out.println("error en ireport");
                    }

                }
                //limpiar los textbox, pone el nuevo numero y lista las facturas
                this.labelnumero.setText(String.valueOf(GestFactura.getInstance().devolverUltimoNum()));
                //       Utilitaria.listarFacturas((DefaultTableModel) jTable1.getModel());
                this.txtCantanimales.setText("");
                this.txtGrupometros.setText("");
                this.txtImporte.setText("");
                this.txtKm.setText("");
                this.txtKmretorno.setText("");
                this.txtCantidadbultos.setText("");
                this.txtGuia.setText("");
                jComboBox2.setSelectedItem(null);
                jComboBox3.setSelectedItem(null);
                jComboBox4.setSelectedItem(null);
            }

        } catch (NullPointerException ex1) {
            JOptionPane.showMessageDialog(rootPane, "Seleccione la fecha antes de agregar la factura");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Hubo un error interno mientras se agregaba la factura. \nSi el problema persiste comuniquese a W.O.R.A inc");

        }
    }//GEN-LAST:event_btnAgregar1ActionPerformed

    private void jComboBox4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        FrmListadoDetallado F = new FrmListadoDetallado();

        F.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        // TODO add your handling code here:
        if (jTable1.getSelectedRow() == 0) {
            if (JOptionPane.showConfirmDialog(null, "Desea eliminar la factura de:  " + String.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 3)) + "  del dia: " + String.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 1))) == 0) {
                int facnro = Integer.parseInt(String.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 0)));
                GestFactura.getInstance().bajaFactura(facnro);
            } else {
                JOptionPane.showMessageDialog(rootPane, "Se cancelo la eliminacion de la factura");
            }


        } else {
            JOptionPane.showMessageDialog(rootPane, "Primero seleccione una factura de la lista");
        }


    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
        try {
            if ("".equals(txtNombre1.getText())) {
                JOptionPane.showMessageDialog(rootPane, "Ingrese numero de factura a buscar");
            } else {
                DefaultTableModel modelo32 = (DefaultTableModel) jTable1.getModel();
                modelo32.setRowCount(0);
                Factura objF = GestFactura.getInstance().devolverFactura(Integer.parseInt(txtNombre1.getText()));
                Object[] objFa = {objF.getfNumero(), objF.getfFecha(), objF.getObjCamion().getcMatricula(), objF.getObjCliente().getpNombre(), objF.getObjEmpleado().getpNombre()};
                modelo32.addRow(objFa);
            }
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(null, "No exsiste tal factura");
            txtNombre1.setText("");
            this.jTable1.setModel(Utilitaria.listarFacturas((DefaultTableModel) jTable1.getModel()));
        }


    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnBuscar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscar1ActionPerformed
        // TODO add your handling code here:
        this.jTable1.setModel(Utilitaria.listarFacturas((DefaultTableModel) jTable1.getModel()));
        this.txtNombre1.setText("");
    }//GEN-LAST:event_btnBuscar1ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        /*Camion camion = (Camion) jTable1.getValueAt(jTable1.getSelectedRow(), 2);
        Empleado chofer = (Empleado) jTable1.getValueAt(jTable1.getSelectedRow(), 4);
        Cliente cliente = (Cliente) jTable1.getValueAt(jTable1.getSelectedRow(), 3);
        jComboBox2.setSelectedItem(camion);
        jComboBox3.setSelectedItem(chofer);
        jComboBox4.setSelectedItem(cliente);*/
        //txtNumero.setText(String.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 0)));
        //jDateChooser1.setDate(Date.parse(jTable1.getValueAt(jTable1.getSelectedRow(), 1)));
    }//GEN-LAST:event_jTable1MouseClicked

    private void jTable1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseExited
        // TODO add your handling code here:
        //this.txtNumero.setText(String.valueOf(GestFactura.getInstance().devolverUltimoNum()));
    }//GEN-LAST:event_jTable1MouseExited

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        if (this.jComboBox1.getSelectedIndex() != 0) {
            this.txttipocambio.setText(String.valueOf(GestCambio.getInstance().devolverUnicoCambio().getDolares()));
        } else {
            this.txttipocambio.setText(String.valueOf(0));
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void txtKmKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtKmKeyReleased
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_txtKmKeyReleased

    private void txtKmKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtKmKeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_txtKmKeyTyped

    private void txtKmretornoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtKmretornoKeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_txtKmretornoKeyTyped

    private void txtImporteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtImporteKeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_txtImporteKeyTyped

    private void txtCantanimalesKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantanimalesKeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_txtCantanimalesKeyTyped

    private void txtGrupometrosKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtGrupometrosKeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_txtGrupometrosKeyTyped

    private void txtCantidadbultosKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidadbultosKeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_txtCantidadbultosKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar1;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnBuscar1;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnatras;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel labelnumero;
    private javax.swing.JTextField txtCantanimales;
    private javax.swing.JTextField txtCantidadbultos;
    private javax.swing.JTextField txtGrupometros;
    private javax.swing.JTextField txtGuia;
    private javax.swing.JTextField txtImporte;
    private javax.swing.JTextField txtKm;
    private javax.swing.JTextField txtKmretorno;
    private javax.swing.JTextField txtLugardecarga;
    private javax.swing.JTextField txtLugardedescarga;
    private javax.swing.JTextField txtNombre1;
    private javax.swing.JTextField txtobservaciones;
    private javax.swing.JTextField txttipocambio;
    // End of variables declaration//GEN-END:variables
}
